<?php 
## include required files
/*******************************/
require_once '../model/users.php';
require_once '../model/moduleuser.php';
## Paginaton class
require_once ("../includes/classes/pagination_class.php");
/*******************************/
## Create Objects
/*******************************/
$userObj = new Model_Users();
$moduleObj=new Model_ModuleUser();
/*******************************/




/*******************************/
##Fetch all admin users from database
/******************************/
if($_GET['q']=='paymenthistory')
{
	$moduelArray=$moduleObj->getModuleHelpText(25);
	$smarty->assign('moduelArray', $moduelArray);
}



## Get search parameters in variables - 
if(trim($_POST['search']) != '' && $_POST['search'] != 'Username') {
//	echo 'aaaaaa';exit;
	$searchindex = addslashes($_POST['search']);
} else if($_GET['search'] != '') {
	$searchindex = addslashes($_GET['search']);
} else {
	$searchindex = '';
}
// Assign search variable to show in search textbox
$smarty->assign('search', stripslashes (stripslashes ($searchindex)));
if($_GET['col1'] != '') 
{
	$col1 = base64_decode($_GET['col1']);
} 
else if($_GET['col2'] != '') 
{
	$col2 = base64_decode($_GET['col2']);
} 
else if($_GET['col3'] != '') 
{
	$col3 = base64_decode($_GET['col3']);
} 

##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;

	if($_GET['col1'] != '') 
	{
		$pageName = "home.php?q=paymenthistory&col1=".$_GET['col1'].""; ;
	} 
	else if($_GET['col2'] != '') 
	{
		$pageName = "home.php?q=paymenthistory&col2=".$_GET['col2'].""; ;
	} 
	else if($_GET['col3'] != '') 
	{
		$pageName = "home.php?q=paymenthistory&col3=".$_GET['col3'].""; ;
	} 
	else 
	{
		$pageName = "home.php?q=paymenthistory"; 
	} 
	## Count all the records
	$userArray = $userObj->getAllTradesmanUsers($searchindex,$col1,$col2,$col3);
	$total_rows = count($userArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##


##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);
	$userArray = $userObj->getAllTradesmanUsers($searchindex,$col1,$col2,$col3, $rowsPerPage,$offset);
	
	if($_GET['task'] == 'exportcsv')
	{
		require_once ("../includes/download.php");	
		$ids = array();
		foreach($userArray as $r)
		{
			$ids[] = $r['id'];
		}
		
		if($_REQUEST['paymentfor']!='0'){
		$paymentfor = addslashes(trim($_REQUEST['paymentfor']));
		} else {
			$paymentfor = '';
		}
		$historyArray  = $userObj->paymentHistoryAccordingToUserIds($ids,$paymentfor,'','',false);	
		$filename = "Payment_history.csv";
		$data = array();
		$temp = array();
		foreach($historyArray[0] as $key=>$value)
		{
			if($key=="id")
				$key="Payment ID";
			$temp[] = $key	;
		}
		$data[] = $temp;
		
		$handler = fopen($filename,"w+");
		fputcsv($handler,$temp);
		foreach($historyArray as $row)
		{
			$temp = array();
			foreach($row as $key=>$val)
				$temp[] = $val;
			fputcsv($handler,$temp);	
		}
	
		
		fclose($handler);
		
		$downloader = new FileDownload();
		$downloader->do_download($filename,"");
		@unlink($filename);
		exit;
	}
	
	//echo "<pre>"; print_r($userArray);exit;
	$smarty->assign('userArray', $userArray);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	} else {
		$other_id = '';
	}
	
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	

## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'paymenthistory');
$smarty->assign('mainmenu', '7');

## Unset all the objects created which are on this page
unset($userObj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'payment/paymenthistory.tpl');
unset($smarty);
?>